package com.demo2do.boyle.backend.web.controller.customer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.customer.CustomerService;
import com.demo2do.boyle.backend.service.system.VariableService;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.CustomerTag;
import com.demo2do.boyle.common.entity.customer.social.SocialAccount;
import com.demo2do.boyle.common.entity.system.Variable;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;

/**
 * 
 * @author mohanlan, Wilson
 *
 */
@Controller
@RequestMapping("/customers")
@MenuSetting("menu-customer")
public class CustomerEditController {

    private static final Logger logger = LoggerFactory.getLogger(CustomerEditController.class);
    
    /** mapping table: variable	id **/
	private static final Integer PROPERTY_TYPE = 1;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private VariableService variableService;
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping("/{id}/profile")
    public ModelAndView profile(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("customer/edit/customer-profile-edit");
        
        Customer customer = customerService.load(Customer.class, id);
        modelAndView.addObject("customer", customer);      
        
        return modelAndView;
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}/profile", method=RequestMethod.PATCH)
    @ResponseBody
    public String onEditProfile(@Preloading( value = "id", type = Integer.class) Customer customer) {
        Result result = new Result();
        try {

            customerService.update(customer);
            result.success().message("更新成功");

        } catch(Exception e) {
            result.fail().message("服务器异常");
            logger.info("onEditProfile()", e);
        }
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * view customer profile
     *
     * @return
     */
     @RequestMapping("/{id}/attr")
     public ModelAndView attr(@PathVariable("id") Integer id) {
         ModelAndView modelAndView = new ModelAndView("customer/customer-attr");
         Customer customer = customerService.load(Customer.class, id);
         
         modelAndView.addObject("customer", customer);
         return modelAndView;
     }

    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/attr/edit", method = RequestMethod.GET)
    public ModelAndView editAttr(@PathVariable("id") Integer id) {

        ModelAndView modelAndView = new ModelAndView("customer/edit/customer-attr-edit");

        Customer customer = customerService.load(Customer.class, id);

        modelAndView.addObject("customer", customer);
        
        return modelAndView;
    }
    
    
    /**
     * Edit customer
     * 
     * @param customer
     * @param ageUnion
     * 		 -description  value code-value like 2-23 
     * @param sexUnion
     * @param areaUnion
     * @param lifeTagCodes
     * @param interestTagCodes
     * @return
     */
    @RequestMapping(value="/{id}/attr/edit", method=RequestMethod.PATCH)
    @ResponseBody
    public String onEditAttr(@Preloading( value = "id", type = Integer.class) CustomerTag customerTag) {
        
    	Result result = new Result();
        
    	try {
    		
    		customerService.editTag(customerTag);
    		
            result.success().message("更新成功");

        } catch(Exception e) {
            result.fail().message("服务器异常");
            logger.info("onEditProfile()", e);
        }
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping("/{id}/social-account/{socialAccountId}/edit")
    public ModelAndView editSocialAccount(@PathVariable("id") Integer customerId, @PathVariable("socialAccountId") Integer socialAccountId) {
        ModelAndView modelAndView = new ModelAndView("customer/edit/social-account-edit");
        
        Customer customer = customerService.load(Customer.class, customerId);
        SocialAccount socialAccount = customerService.loadSocialAccount(socialAccountId);
        Variable variable = variableService.queryObjectForId(PROPERTY_TYPE);
        
        modelAndView.addObject("socialAccount", socialAccount);
        modelAndView.addObject("customer", customer);
        modelAndView.addObject("variable", variable);
        modelAndView.addObject("type", socialAccount.getType().name());
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value="/{id}/social-account/{socialAccountId}", method=RequestMethod.PATCH)
    @ResponseBody
    public String onEditSocialAccount(@PathVariable("socialAccountId") Integer id, @RequestParam("nickName") String nickName, @RequestParam("fans") int fans) {

    	SocialAccount socialAccount = customerService.loadSocialAccount(id);
        Result result = customerService.editSocialAccount(socialAccount.changeFansAndNickName(fans,nickName));

        return JsonUtils.toJsonString(result);
    }

}
